﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using FILSTARTUPS_CMS.Models;
using System.Security.Cryptography;
using System.Data.SqlTypes;

namespace FILSTARTUPS_CMS.Helpers
{

    public class Session_Helper
    {
        FilStartUpsEntities cms_dbconnect = new FilStartUpsEntities();
        // Session_Helpers session = new Session_Helpers();
        Session session = new Session();
        public static string Truncate(string description, int lenght)
        {
            if (description.Length < lenght)
            {
                return description;
            }
            else
            {
                return description.Substring(0, lenght) + "...";
            }
        }

        public bool CreateSession(string username)
        {
            DateTime mindate = DateTime.MinValue;
            var user = cms_dbconnect.Users.Where(u => u.Username == username).SingleOrDefault();
            session.useridFK = user.useridPK;
            session.session_flag = true;
            session.session_datelogon = DateTime.Now;
            session.session_datelogout = Convert.ToDateTime(SqlDateTime.MinValue.Value);
            cms_dbconnect.Sessions.Add(session);
            cms_dbconnect.SaveChanges();
            return true;
        }
        public bool CheckSession(string username)
        {
            var user = cms_dbconnect.Users.Where(u => u.Username == username).SingleOrDefault();
            var session_user = cms_dbconnect.Sessions.Where(s => s.useridFK == user.useridPK && s.session_flag == true).SingleOrDefault();

            if (session_user != null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public void UpdateSession(int userid)
        {
            var session_log = cms_dbconnect.Sessions.Where(s => s.useridFK == userid && s.session_flag == true).SingleOrDefault();
            session_log.session_datelogon = DateTime.Now;
            cms_dbconnect.Entry(session_log).State = EntityState.Modified;
            cms_dbconnect.SaveChanges();
        }

        public void AbandonSession(string username)
        {

            var user = cms_dbconnect.Users.Where(u => u.Username == username).SingleOrDefault();
            var sessionlog = cms_dbconnect.Sessions.Where(s => s.useridFK == user.useridPK && s.session_flag == true).SingleOrDefault();
            sessionlog.session_datelogout = DateTime.Now;
            sessionlog.session_flag = false;
            cms_dbconnect.Entry(sessionlog).State = EntityState.Modified;
            cms_dbconnect.SaveChanges();

        }

        public static string HashPassword(string password)
        {

            SHA1 hash = SHA1.Create();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] combined = encoder.GetBytes(password);
            hash.ComputeHash(combined);
            return Convert.ToBase64String(hash.Hash);

        }
    }
}